﻿/// <reference path="../Scripts/jquery-1.8.1-vsdoc.js" />

$.ajaxSetup({
    url: "/Handler/Patent.ashx?" + (new Date()).valueOf(),
    type: "POST",
    dataType: "text",
    global: false,
});

$(document).ready(function () {
    BindDepartment();
    $("#btnSave").bind("click", Save);
    $("#btnNext").bind("click", Save);
    $("#btnDel").bind("click", Del);
    var ID = $("#MainContent_ID").val();
    if (!Util.isEmpty(ID)) {
        $("#btnDel").show();  
        BindData(ID);
    }
});

function BindDepartment()
{
        $.ajax({
            url: "/Handler/Department.ashx?" + (new Date()).valueOf(),
            async:false,
            contentType:'application/x-www-form-urlencoded;charset=gb2312',
            data: {action:"loadData"},
            success: function (data) {
            var ret=$.parseJSON(data).Departments;
            var selects= $("#Department");
            selects.empty();
            selects.append("<option value='-1'>-请选择-</option>"); 
            for (var i=0;i<ret.length;i++) 
            {
                selects.append("<option value='"+decodeURIComponent(ret[i].ID)+"'>"+decodeURIComponent(ret[i].Name)+"</option>"); 
            }
        }
    });
}

function Save() {    
    if(!IsValidate())
    {
        return;
    }
    var ret=GetData();
    var jsonStr=$.toJSON(ret);
    $.ajax({
        contentType:'application/x-www-form-urlencoded;charset=gb2312',
        data: {action:Util.isEmpty(ret.ID)?"addData":"updateData",jsonDataString:jsonStr},
        beforeSend: function () {
            Util.blockUI("正在保存数据...");
        },
        success: function (data) {
            Util.unblockUI();
            var obj=$.parseJSON(data);
            Util.freshPage("PatentDetail.aspx?ID="+obj.MSG);
        }
    });
}

function Del()
{
    if(!confirm("确定删除?"))
    {
       return;
    }

    var ret=
    {
       "ID":encodeURIComponent($("#MainContent_ID").val())
    }
    var jsonStr=$.toJSON(ret);
    $.ajax({
        contentType:'application/x-www-form-urlencoded;charset=gb2312',
        data: {action:"removeData",jsonDataString:jsonStr},
        beforeSend: function () {
            Util.blockUI("正在删除数据...");
        },
        success: function (data) {
            Util.unblockUI();
            Util.freshPage("PatentList.aspx");
        }
    });
}

function GetData() {
    var ret=
    {
       "ID":encodeURIComponent($("#MainContent_ID").val()),
       "No":encodeURIComponent($('#No').val()),   
       "RequestNo":encodeURIComponent($('#RequestNo').val()),   
       "Name": encodeURIComponent($("#Name").val()),
       "RequestDate": encodeURIComponent($("#RequestDate").val()),
       "ReceiveDate": encodeURIComponent($("#ReceiveDate").val()),
       "DepID":encodeURIComponent($("#Department").find("option:selected").val()),
       "Type": encodeURIComponent($("#Type").find("option:selected").val()),
       "Introduce": encodeURIComponent($("#Introduce").val()),
       "Memo": encodeURIComponent($("#Memo").val()),
       "LastModifiedBy":encodeURIComponent($("#MainContent_UserID").val()),
    }

    return ret;
}

function IsValidate() {
    var msg="";
    if (Util.isEmpty($('#Name').val())) {
        msg="专利名称不能为空!";
    }
    else if (Util.isEmpty($('#No').val())) {
        msg="专利号不能为空!";
    }
    else if (Util.isEmpty($('#ReceiveDate').val())) {
        msg="专利授权时间不能为空!";
    }
    else if ($('#Department').val()=='-1') {
        msg="请选择专利所属部门!";
    }
    else if (Util.isEmpty($('#Type').val())) {
        return "专利类别不能为空!";
    }

    if(Util.isEmpty(msg))
    {
        return true;
    }
    else
    {
        alert(msg);
        return false;
    }
}

 function compareDateTime(kssj,jssj) {
    var kssjArr = kssj.split("-");
    var jssjArr = jssj.split("-");
    var date1 = new Date(kssjArr[0], kssjArr[1], kssjArr[2]);
    var date2 = new Date(jssjArr[0], jssjArr[1], jssjArr[2]);
    if (date1 > date2) return 1;
    else return 0;
}

function BindData(ID) {
    $.ajax({
            contentType:'application/x-www-form-urlencoded;charset=gb2312',
            data: {action:"loadData",id:ID},
            beforeSend: function () {
                Util.blockUI("正在加载数据...");
            },
            success: function (data) {
                Util.unblockUI();
                var obj=$.parseJSON(data).Patents[0];
                $("#MainContent_ID").val(decodeURIComponent(obj.ID));
                $("#No").val(decodeURIComponent(obj.No));
                $("#RequestNo").val(decodeURIComponent(obj.RequestNo));
                $("#Name").val(decodeURIComponent(obj.Name));
                $("#RequestDate").val(Util.changeDateFormat(decodeURIComponent(obj.RequestDate)));
                $("#ReceiveDate").val(Util.changeDateFormat(decodeURIComponent(obj.ReceiveDate)));
                SelectedValue("Department",decodeURIComponent(obj.DepID));
                SelectedValue("Type",decodeURIComponent(obj.Type));
                $("#Introduce").val(decodeURIComponent(obj.Introduce));
                $("#Memo").val(decodeURIComponent(obj.Memo));
            }
        });
}